package com.oa.service.user.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.mapper.user.AttendCountMapper;
import com.oa.pojo.AttendanceCount;
import com.oa.service.user.AttendanceCountService;
import com.oa.vo.AttendanceVO;
import com.oa.vo.StackedLineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 */
@Service
public class AttendanceCountImpl implements AttendanceCountService {
    @Autowired
    private AttendCountMapper attendCountMapper;

    /**
     * 生成每日的出勤统计数据
     *
     * @param day 选择天数
     * @return
     */
    @Override
    public JSON generateAttdanceByday(String day) {
        ArrayList<AttendanceVO> countsList = new ArrayList<>();
        DateTime dateTime = DateUtil.parse(day);
        Integer attendanceCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("attendance_day", dateTime).eq("attendance_num", 1));
        Integer absentCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("attendance_day", dateTime).eq("absent_num", 1));
        Integer busCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("attendance_day", dateTime).eq("bus_num", 1));
        Integer leaveCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("attendance_day", dateTime).eq("leave_num", 1));
        countsList.add(new AttendanceVO().setValue(attendanceCount).setName("正常出勤"));
        countsList.add(new AttendanceVO().setValue(absentCount).setName("异常出勤"));
        countsList.add(new AttendanceVO().setValue(busCount).setName("出差出勤"));
        countsList.add(new AttendanceVO().setValue(leaveCount).setName("请假"));
        JSON json = JSONUtil.parse(countsList);
        return json;
    }

    /**
     * 生成个人的出勤统计
     *
     * @param empId
     * @return
     */
    @Override
    public JSON generateAttdanceByUser(Integer empId) {
        ArrayList<AttendanceVO> countsList = new ArrayList<>();
        Integer attendanceCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("emp_id", empId).eq("attendance_num", 1));
        Integer absentCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("emp_id", empId).eq("absent_num", 1));
        Integer busCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("emp_id", empId).eq("bus_num", 1));
        Integer leaveCount = attendCountMapper.selectCount(new QueryWrapper<AttendanceCount>().eq("emp_id", empId).eq("leave_num", 1));
        countsList.add(new AttendanceVO().setValue(attendanceCount).setName("正常出勤"));
        countsList.add(new AttendanceVO().setValue(absentCount).setName("异常出勤"));
        countsList.add(new AttendanceVO().setValue(busCount).setName("出差出勤"));
        countsList.add(new AttendanceVO().setValue(leaveCount).setName("请假"));
        JSON json = JSONUtil.parse(countsList);
//        System.out.println(countsList);
        return json;
    }

    /**
     * 根据用户查询所有一个月的考情统计信息返回数据
     *
     * @param empId
     * @return
     */
    @Override
    public List<AttendanceCount> getAttendCountListByUser(Integer empId) {
        Date date = new Date();
        Date startDay = DateUtil.beginOfMonth(date);
        Date endDay = DateUtil.endOfMonth(date);
        ArrayList<Date> list = new ArrayList<>();
        while (startDay.before(endDay)) {
            list.add(startDay);
            startDay = DateUtil.offsetDay(startDay, 1);
        }
        List<AttendanceCount> attendanceCounts = attendCountMapper.selectList(new QueryWrapper<AttendanceCount>()
                .eq("emp_id", empId)
                .in("attendance_day", list));
        for (AttendanceCount attendanceCount : attendanceCounts) {
            Date attendanceDay = attendanceCount.getAttendanceDay();
            attendanceCount.setMonths(DateUtil.format(attendanceDay, "MM"))
                    .setDays(DateUtil.format(attendanceDay, "dd"));
        }
        return attendanceCounts;
    }

    /**
     * 按周统计考勤的信息
     *
     * @param date 选择的日期
     * @return 返回按照图表格式封装的对象
     */
    @Override
    public List<StackedLineVO> getStackedLineData(String date) {
        DateTime parse = DateUtil.parse(date, "yyyy-MM-dd");
        ArrayList<Date> dates = new ArrayList<>();
        Date dateTime = DateUtil.beginOfWeek(parse);
        List<StackedLineVO> stackedLineVOS = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            dates.add(dateTime);
            dateTime = DateUtil.offsetDay(dateTime, 1);
        }
        List<AttendanceCount> busCounts = attendCountMapper.selectList(new QueryWrapper<AttendanceCount>().eq("bus_num", 1).in("attendance_day", dates));
        List<AttendanceCount> attCounts = attendCountMapper.selectList(new QueryWrapper<AttendanceCount>().eq("attendance_num", 1).in("attendance_day", dates));
        List<AttendanceCount> absentCounts = attendCountMapper.selectList(new QueryWrapper<AttendanceCount>().eq("absent_num", 1).in("attendance_day", dates));
        List<AttendanceCount> leaveCounts = attendCountMapper.selectList(new QueryWrapper<AttendanceCount>().eq("leave_num", 1).in("attendance_day", dates));
        //buscount
        ArrayList<Integer> list = new ArrayList<>();
        //attcount
        ArrayList<Integer> list1 = new ArrayList<>();
        //absentcount
        ArrayList<Integer> list2 = new ArrayList<>();
        //leavecount
        ArrayList<Integer> list3 = new ArrayList<>();
        for (Date day : dates) {
            int attcount =0;
            int buscount =0;
            int absentcount =0;
            int leavecount =0;
            for (AttendanceCount busCount : busCounts) {
                if (busCount.getAttendanceDay().equals(day)){
                    buscount++;
                }
            }
            for (AttendanceCount attendanceCount : attCounts) {
                if (attendanceCount.getAttendanceDay().equals(day)){
                    attcount++;
                }
            }
            for (AttendanceCount absentCount : absentCounts) {
                if (absentCount.getAttendanceDay().equals(day)){
                    absentcount++;
                }
            }
            for (AttendanceCount leaveCount : leaveCounts) {
                if (leaveCount.getAttendanceDay().equals(day)){
                    leavecount++;
                }
            }
            list.add(buscount);
            list1.add(attcount);
            list2.add(absentcount);
            list3.add(leavecount);
        }
        stackedLineVOS.add(new StackedLineVO().setName("出差").setStack("Total").setType("line").setData(list));
        stackedLineVOS.add(new StackedLineVO().setName("正常出席").setStack("Total").setType("line").setData(list1));
        stackedLineVOS.add(new StackedLineVO().setName("异常出勤").setStack("Total").setType("line").setData(list2));
        stackedLineVOS.add(new StackedLineVO().setName("请假").setStack("Total").setType("line").setData(list3));
        return stackedLineVOS;
    }
}
